const kingDeeApi = require("../kingdeeOldApi");
const redisUtil = require('../../utils/res-util');
const kingdeeAuthToken = require('../../../libs-jdy/openapi/kingdeeAuthToken');
const cryptoUtil = require('../../utils/crypto-util');


/**
 * 配送中心-合顺商贸
 * 登录接口
 * @param ctx
 * @returns {Promise<void>}
 */
const loginISV = async (ctx) => {
    let kingdeeurl = '/discentershssm/login';
    const params = await ctx.request.body;
    if (!(params.mobile || params.password)) {
        console.error(' miniLogin ', '用户名或密码为空');
        ctx.body = Res.error(Mes.USER_PASSWORD_ERROR);
        return;
    }
    console.log('小程序登录用户名', params.mobile);
    await Promise.resolve().then(async () => {
        try {
            ctx.request.headers.kingdeeurl = kingdeeurl;
            ctx.request.body.password = await cryptoUtil.md5(params.password);
            ctx.request.body.mobile = await params.mobile;
            await kingDeeApi.kingdeeOldApi(ctx);
        } catch (error) {
            ctx.body = Res.error("接口返回失败", {
                code: error.response.data.errcode,
                message: error.response.data.description
            });
        }

    })
}

/**
 * 配送中心-合顺商贸
 * 查询拣货单
 * @param ctx
 * @returns {Promise<void>}
 */
const pickingslip = async (ctx) => {
    let kingdeeurl = '/discentershssm/get_pickingslip';
    await Promise.resolve().then(async () => {
        try {
            ctx.request.headers.kingdeeurl = kingdeeurl;
            await kingDeeApi.kingdeeOldApi(ctx);
        } catch (error) {
            ctx.body = Res.error("查询拣货单接口失败", {
                code: error.response.data.errcode,
                message: error.response.data.description
            });
        }

    })
}


/**
 * 配送中心-合顺商贸
 * 核验确认【更新销售订单与商品分录信息】
 * @param ctx
 * @returns {Promise<void>}
 */
const updateSalOrderList = async (ctx) => {
    let kingdeeurl = '/discentershssm/update_sal_order_list';
    await Promise.resolve().then(async () => {
        try {
            ctx.request.headers.kingdeeurl = kingdeeurl;
            await kingDeeApi.kingdeeOldApi(ctx);
        } catch (error) {
            ctx.body = Res.error("核验确认【更新销售订单与商品分录信息】", {
                code: error.response.data.errcode,
                message: error.response.data.description
            });
        }
    })
}

module.exports = {
    loginISV,
    pickingslip,
    updateSalOrderList
};